package com.itheima.job;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test04 {
    public static void main(String[] args) throws Exception{
        Class clazz = Class.forName("com.itheima.job.Student");
        Student stu = (Student) clazz.newInstance();
        setProperty(stu,"setName","张三");
        Object name = getProperty(stu, "getName");
        System.out.println(name);

    }
    //此方法可将obj对象中名为propertyName的属性的值设置为value
    public static void setProperty(Object obj, String propertyName, Object value) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        Class clazz = obj.getClass();
        Method method = clazz.getMethod(propertyName, String.class);
        method.invoke(obj,value);
    }

    //此方法可以获取obj对象中名为propertyName的属性的值
    public static Object getProperty(Object obj, String propertyName) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        Class clazz = obj.getClass();
        Method method = clazz.getMethod(propertyName);

        return method.invoke(obj);
    }
}
